package core

import (
	"fmt"
	"io/ioutil"
	"path/filepath"

	"gopkg.in/yaml.v2"
)

var Config = &Configuration{}

type Configuration struct {
	Application struct {
		Port   int    `yaml:"port"`
		Host   string `yaml:"Host"`
		Salt   string `yaml:"salt"`
		Static struct {
			Path string `yaml:"path"`
			URL  string `yaml:"url"`
		}
		Redis struct {
			Addr     string `yaml:"addr"`
			Password string `yaml:"password"`
			DB       int    `yaml:"db"`
		}
		Database struct {
			Host     string `yaml:"host"`
			Port     int    `yaml:"port"`
			User     string `yaml:"user"`
			Password string `yaml:"password"`
			DBName   string `yaml:"dbname"`
		}
	}
}

func init() {
	configPath, err := filepath.Abs("config/application.yaml")
	if err != nil {
		fmt.Printf("Failed to get config file path: %v\n", err)
		return
	}

	file, err := ioutil.ReadFile(configPath)
	if err != nil {
		fmt.Printf("Failed to read config file: %v\n", err)
		return
	}

	if err := yaml.Unmarshal(file, Config); err != nil {
		fmt.Printf("Failed to parse config file: %v\n", err)
		return
	}
}
